namespace NHCodeFirst.FluentMapper
{
    using System;
    using Enums;
    using HbmModel;

    public class OneToManyMapper<TMany>
    {
        private readonly Collection collection;

        internal OneToManyMapper(Collection collection)
        {
            this.collection = collection;
        }

        public OneToManyMapper<TMany> Inverse(bool value)
        {
            collection.Inverse = value.ToString().ToLower();
            return this;
        }

        public OneToManyMapper<TMany> Cascade(CollectionCascade cascade)
        {
            switch (cascade)
            {
                case CollectionCascade.AllDeleteOrphan:
                    collection.Cascade = "all-delete-orphan";
                    break;
                case CollectionCascade.SaveUpdate:
                    collection.Cascade = "save-update";
                    break;
                default:
                    collection.Cascade = cascade.ToString().ToLower();
                    break;
            }
            return this;
        }
        
        public OneToManyMapper<TMany> MapKeys(Action<KeyMapper<TMany>> mapper)
        {
            if (collection.Key == null)
                collection.Key = new Key();
            mapper(new KeyMapper<TMany>(collection.Key));
            return this;
        }

        public OneToManyMapper<TMany> EntityName(string value)
        {
            collection.OneToMany.EntityName = value;
            return this;
        }

        public OneToManyMapper<TMany> NotFound(NotFound value)
        {
            collection.OneToMany.NotFound = value.ToString().ToLower();
            return this;
        }
    }
}